package com.csust.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.csust.reggie.common.BaseContext;
import com.csust.reggie.common.R;
import com.csust.reggie.domain.ShoppingCart;
import com.csust.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车模块
 * @author yinweiguang
 * @date 2022-04-05 16:48
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private IShoppingCartService shoppingCartService;


    @PostMapping("/sub")
    public R<String> update(@RequestBody ShoppingCart shoppingCart) {
        // 修改购物车信息
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        // 判断当前修改的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // 查询当前用户的购物车信息
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        // 修改购物车的数量
        Integer number = cartServiceOne.getNumber();
        // 如果数量为1，减一之后为0 直接删除当前的记录
        if (number > 1) {
            // 数量减一
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            // 删除当前记录操作的数据
            shoppingCartService.remove(queryWrapper);
        }
        return R.success("修改成功");
    }


    /**
     *  清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清除购物车成功");
    }


    /**
     * 查看购物城
     * @return
     */
    @GetMapping("/list")
    public R< List<ShoppingCart>> list () {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if(dishId != null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else{
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询当前菜品或者套餐是否在购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne != null){
            //如果已经存在，就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            // 设置入库时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne);
    }
//    @PostMapping("/add")
//    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
//        log.info("shoppingCart = {}",shoppingCart);
//        // 获取用户的id
//        Long user_id = BaseContext.getCurrentId();
//        shoppingCart.setUserId(user_id);
//        // 查询当前菜品或套餐是否在购物车里面
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId,user_id);
//        queryWrapper.eq(null != shoppingCart.getDishId(),ShoppingCart::getDishId,shoppingCart.getDishId());
//        queryWrapper.eq(null != shoppingCart.getSetmealId(),ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
//        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);
//        // 如果存在，就在当前购物车的数量上 加 1
//       if (cart != null) {
//           Integer number = cart.getNumber();
//           cart.setNumber(number + 1);
//           shoppingCartService.updateById(cart);
//       } else {
//           // 如果不存在，数量默认为1
//           shoppingCart.setNumber(1);
//           shoppingCartService.save(shoppingCart);
//           cart = shoppingCart;
//       }
//        return R.success(cart);
//    }
}
